﻿
package system.base;

import java.util.Vector;

import framework.core.*;
import system.data.*;
/*
	Author    :Benny
	CreateTime:2008-05-19 11:26:57
*/
public class BusMaterialAccountKilSrv extends IHandler
{

	public BusMaterialAccountKilSrv() 
	{
		super();
	}
	private void deleteBusMaterialAccountKil(DBConnection con,SrvContext context) throws Exception
	{
		String[] ids = context.getParameterValues("ids");
		BusMaterialAccountKil obj = null;
		for(int i=0;i<ids.length;i++)
		{
			obj = BusMaterialAccountKil.fetch(con,ids[i]);
			if(obj!=null) obj.delete(con);
		}
	}
	private void saveBusMaterialAccountKil(DBConnection con,SrvContext context,ILoginContext loginContext) throws Exception
	{
		String id = context.getString("Id",null);
		BusMaterialAccountKil obj;
		if((id==null)||(id.equals("")))   //add
		{
			obj = BusMaterialAccountKil.fetch();
    	}
    	else                     //update
    	{
    		obj = BusMaterialAccountKil.fetch(con,id);
    	}
		BusMaterialAccountKilPageData data = (BusMaterialAccountKilPageData)PageData.fromForm(context,BusMaterialAccountKilPageData.class);
		context.setValue("BusMaterialAccountKilPageData",data);
		obj.fill(data);
		obj.save(con);
	}
	/** 从集合中取出数组对象  并且将指定的索引位置的值取出  组成一个新的数组*/
	public static String[] getStr(Vector vec,int index){
        String[] values = new String[vec.size()];
        for (int i = 0; i < vec.size(); i++) {
            String[] str = (String[])vec.get(i);
            values[i] = str[index];
        }
        return values;
    }
	
	private void listBusMaterialAccountKil(DBConnection con,SrvContext context,String path) throws Exception
	{
        BusMaterialAccountKilSearchEvent event = (BusMaterialAccountKilSearchEvent)SearchEvent.fromForm(context,BusMaterialAccountKilSearchEvent.class);
        String warehouseCode = "";
        String repairedTime = "";
        boolean isfirst = checkFirst(context);
        if(isfirst){
            event.setWarehouseCode("000000");
            event.setRepairenTime("000000");
            warehouseCode = "000000";
            repairedTime = "000000";
        }else{
            warehouseCode = context.getString("s_WarehouseCode") == null ? "" : context.getString("s_WarehouseCode");
            repairedTime = context.getString("s_RepairenTime") == null ? "" : context.getString("s_RepairenTime");
        }
        
        context.setSessionValue("BusMaterialAccountKilSearchEvent",event);
        //放置首次登陆判断结果
        context.setSessionValue("isfirst",Boolean.valueOf(isfirst));
        //获得点击的菜单编码
        String menuCode = context.getString("menu");
        //如果编码空则在session中提取
        if(menuCode == null || menuCode == ""){
            menuCode = (String)context.getSessionValue("choice");
        }else{
            // 为返回请求确定菜单方法
            context.setSessionValue("choice", menuCode);
        }
        // 转换为整形
        int menu = Integer.parseInt(menuCode);
        
        //判断点击菜单
        switch (menu) {
        //各公司分车型单车材料费用
        case 6002:
            if(!isfirst){
                
                String sql = "select * from T_BUS_MATERIAL_ACCOUNT_KIL where WAREHOUSE_CODE = '"+warehouseCode+"' and REPAIREN_TIME like '"+repairedTime+"%'";
                //提取查询结果    此为基础数据  不完全
                Vector vec = BusMaterialAccountKil.getFullData(sql, con);
                
                //将结果放置request作用域
                context.setValue(menuCode, vec);
            }
            //设置跳转

            context.setNextUrl(path+"BusMaterialAccountKilList6002.jsp");
            break;
        case 6003:
            if(!isfirst){
                String sql = "select * from T_BUS_MATERIAL_ACCOUNT_KIL where WAREHOUSE_CODE = '"+warehouseCode+"' and REPAIREN_TIME like '"+repairedTime+"%'";
              //提取查询结果    此为基础数据  不完全
                Vector vec = BusMaterialAccountKil.getFullData(sql, con);
                
                //将结果放置request作用域
                context.setValue(menuCode, vec);
            }
            //设置跳转

            context.setNextUrl(path+"BusMaterialAccountKilList6003.jsp");
            break;
        case 6006:
            if(!isfirst){
                String sql = "select * from T_BUS_MATERIAL_ACCOUNT_KIL where REPAIREN_TIME like '"+repairedTime+"%'";
                //提取查询结果    此为基础数据  不完全
                Vector vec = BusMaterialAccountBusiness.getFullData(sql, con);
                //将结果放置request作用域
                context.setValue(menuCode, vec);
            }
            //设置跳转
            context.setNextUrl(path+"BusMaterialAccountKilList6006.jsp");
            break;
        default:
            break;
        case 6007:
            if(!isfirst){
                String sql = "select * from T_BUS_MATERIAL_ACCOUNT_KIL where REPAIREN_TIME like '"+repairedTime+"%'";
              //提取查询结果    此为基础数据  不完全
                Vector vec = BusMaterialAccountBusiness.getFullData(sql, con);
                //将结果放置request作用域
                context.setValue(menuCode, vec);
            }
            //设置跳转
            context.setNextUrl(path+"BusMaterialAccountKilList6007.jsp");
            break;
            }
        }
	
	private void backBusMaterialAccountKil(DBConnection con,SrvContext context,String path) throws Exception
	{
		context.setNextUrl(path+"BusMaterialAccountKilList.jsp");
	}
	
	public void doAction(DBConnection con,SrvContext context,ILoginContext loginContext) throws Exception
	{
		String action_code = context.getActionCode();
		String path = "/system/base/";
		
		if(action_code.equalsIgnoreCase("listBusMaterialAccountKil"))
		{
			listBusMaterialAccountKil(con,context,path);
			return;
		}
		if(action_code.equalsIgnoreCase("viewBusMaterialAccountKil"))
		{
			String id = context.getString("Id",null);
			BusMaterialAccountKil obj = BusMaterialAccountKil.fetch(con,id);
			context.setValue("BusMaterialAccountKil",obj);
			context.setNextUrl(path+"BusMaterialAccountKilView.jsp");
			return;
		}
		if(action_code.equalsIgnoreCase("deleteBusMaterialAccountKil"))
		{
			context.setErrorUrl(path+"BusMaterialAccountKilList.jsp");
			deleteBusMaterialAccountKil(con,context);
			backBusMaterialAccountKil(con,context,path);
			return;
		}
			
		if(action_code.equalsIgnoreCase("toAddBusMaterialAccountKil"))
		{
			BusMaterialAccountKil obj = BusMaterialAccountKil.fetch();
			BusMaterialAccountKilPageData data = (BusMaterialAccountKilPageData)PageData.fromTable(obj,BusMaterialAccountKilPageData.class);
			context.setValue("BusMaterialAccountKilPageData",data);
			context.setNextUrl(path+"BusMaterialAccountKilEdit.jsp");
			return;
		}
		if(action_code.equalsIgnoreCase("toUpdateBusMaterialAccountKil"))
		{
			String id = context.getString("Id",null);
			BusMaterialAccountKil obj = BusMaterialAccountKil.fetch(con,id);
			BusMaterialAccountKilPageData data = (BusMaterialAccountKilPageData)PageData.fromTable(obj,BusMaterialAccountKilPageData.class);
			context.setValue("BusMaterialAccountKilPageData",data);
			context.setNextUrl(path+"BusMaterialAccountKilEdit.jsp");
			return;
		}
		if(action_code.equalsIgnoreCase("toCopyBusMaterialAccountKil"))
		{
			String id = context.getString("Id",null);
			BusMaterialAccountKil obj = BusMaterialAccountKil.copy(BusMaterialAccountKil.fetch(con,id));
			BusMaterialAccountKilPageData data = (BusMaterialAccountKilPageData)PageData.fromTable(obj,BusMaterialAccountKilPageData.class);
			context.setValue("BusMaterialAccountKilPageData",data);
			context.setNextUrl(path+"BusMaterialAccountKilEdit.jsp");
			return;
		}
		if(action_code.equalsIgnoreCase("saveBusMaterialAccountKil"))
		{
			context.setErrorUrl(path+"BusMaterialAccountKilEdit.jsp");
			saveBusMaterialAccountKil(con,context,loginContext);
			backBusMaterialAccountKil(con,context,path);
			return;
		}
		if(action_code.equalsIgnoreCase("saveAndNewBusMaterialAccountKil"))
		{
			context.setErrorUrl(path+"BusMaterialAccountKilEdit.jsp");
			saveBusMaterialAccountKil(con,context,loginContext);
			BusMaterialAccountKil obj = BusMaterialAccountKil.fetch();
			BusMaterialAccountKilPageData data = (BusMaterialAccountKilPageData)PageData.fromTable(obj,BusMaterialAccountKilPageData.class);
			context.setValue("BusMaterialAccountKilPageData",data);
			context.setNextUrl(path+"BusMaterialAccountKilEdit.jsp");			
			return;
		}
		if(action_code.equalsIgnoreCase("backListBusMaterialAccountKil"))
		{
			backBusMaterialAccountKil(con,context,path);
			return;
		}
		
		//add by Benny start

		//add by Benny end

		Logger.throwException("BusMaterialAccountKilSrv","unknown action_code="+action_code);
	}
	private boolean checkFirst(SrvContext context) throws Exception{
        boolean isfirst = context.getBoolean("isfirst");
        return isfirst;
    }
}
